export function handleDate (format, value = '') {
  if (!value) return '--'
  let [_type, _date] = [format, new Date(value)]
  return _type
    .replace(/(y{4})/, _date.getFullYear())
    .replace(/(M{2})/, () => _date.getMonth() < 9 ? '0' + (_date.getMonth() + 1) : (_date.getMonth() + 1).toString())
    .replace(/d{2}/, () => _date.getDate() < 10 ? '0' + (_date.getDate()) : _date.getDate().toString())
    .replace(/H{2}/, () => _date.getHours() < 10 ? '0' + (_date.getHours()) : _date.getHours().toString())
    .replace(/m{2}/, () => _date.getMinutes() < 10 ? '0' + (_date.getMinutes()) : _date.getMinutes().toString())
    .replace(/s{2}/, () => _date.getSeconds() < 10 ? '0' + (_date.getSeconds()) : _date.getSeconds().toString())

}

export function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  console.log(r)
  if (r != null) return unescape(r[2]);
  return null;
}

export const local = {
  get (name) {
    let value = localStorage.getItem(name)
    if (/^\{.*\}$/.test(value)) value = JSON.parse(value)
    return value
  },
  set (name, value) {
    if (typeof value === typeof {}) value = JSON.stringify(value)
    return localStorage.setItem(name, value)
  },
  remove (name) {
    return localStorage.removeItem(name)
  }
}

